package cn.xej.health.team.controller;

import cn.xej.health.common.utils.CommonResult;
import cn.xej.health.common.utils.PageUtils;
import cn.xej.health.common.utils.Result;
import cn.xej.health.team.entity.Division;
import cn.xej.health.team.entity.Team;
import cn.xej.health.team.feign.UserFeignService;
import cn.xej.health.team.service.TeamService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author xej
 */
@RefreshScope
@RestController
@RequestMapping("/team")
public class TeamController {

    @Autowired
    private TeamService teamService;

    @Autowired
    private UserFeignService userFeignService;

    @GetMapping("/list")
    public CommonResult list(@RequestParam("pageIndex")Integer pageIndex,@RequestParam("pageSize")Integer pageSize,@RequestParam("searchName")String searchName){
        return teamService.queryTeamList(pageIndex,pageSize,searchName);
    }

    @PostMapping("/add")
    public CommonResult add(@RequestBody Team team){
        team.setCreateDate(new Date());
        teamService.save(team);
        return CommonResult.success(null);
    }


    @GetMapping("/info/{tid}")
    public CommonResult info(@PathVariable("tid")Integer tid){
        Team team = teamService.getById(tid);
        return CommonResult.success(team);
    }

    @PostMapping("/delete")
    public CommonResult delete(@RequestBody Integer[] tids){
        List<Integer> list = Arrays.asList(tids);
        boolean flag = teamService.removeByIds(list);
        if (flag){
            return CommonResult.success(null);
        }else {
            return CommonResult.failed();
        }
    }


    @PutMapping("/update")
    public CommonResult update(@RequestBody Team team){
        teamService.updateById(team);
        return CommonResult.success(null);
    }

    @GetMapping("/check/{captainId}")
    public CommonResult check(@PathVariable("captainId")Integer captainId){
        return userFeignService.getUser(captainId);
    }

    @GetMapping("/getTeam/{tid}")
    public CommonResult getTeam(@PathVariable("tid")Integer tid){
        Team team = teamService.getById(tid);
        return CommonResult.success(team);
    }

    @GetMapping("/getTeamByUserId/{uuid}")
    public CommonResult getTeamByUserId(@PathVariable("uuid")Integer uuid){
        Team team = teamService.getOne(new QueryWrapper<Team>().eq("CAPTAIN_ID", uuid));
        return CommonResult.success(team);
    }

    @GetMapping("/captainList")
    public CommonResult captainList(){
        return userFeignService.captainList();
    }

    @GetMapping("/divisionList")
    public CommonResult divisionList(){
        return teamService.queryDivisionList();
    }


    @GetMapping("/teamTree/{did}")
    public CommonResult teamTree(@PathVariable("did")Integer did){
        return teamService.queryTeamTree(did);
    }
}
